/**
  项目JS主入口
  以依赖layui的layer和form模块为例
**/
layui.define(['layer', 'form', 'jquery', 'element'], function(exports) {
	var layer = layui.layer,
		$ = layui.jquery,
		element = layui.element,
		form = layui.form;
	var obj = {
		top_nav: function() {
			var top_nav_document;
			var count_size;
			var index;
			$("#header .layui-nav li a").hover(function() {
				index = $(this).parents("li").index(); //找到当前元素的位置
				top_nav_document = $(this).parents("li").siblings("span");
				count_size = $(this).parents("li").width() * index + $(this).width();
				top_nav_document.css({
					"width": $(this).parents("li").width() / 2,
					"opacity": 1,
					"left": count_size,
					"transition": "all .3s",
					"height": 1,
					"background-color": "#35495e"
				});
			}, function() {
				top_nav_document.css({
					"opacity": 0
				});
			});
		},
		fitPage:function () {
			var width = $(window).width();
			if (width>1366){
				return 15;
			}else{
				return 10;
			}
        },
		shrink: function() {
			$("#shrink-nav").click(function() {
				var fig = layui.data('nav')['fig'];
				if(fig) {
					layui.data("nav", {
						key: "fig",
						value: false
					});
				} else {
					layui.data("nav", {
						key: "fig",
						value: true
					});
				}
				obj.shrink_nav();
			});
		},
		shrink_nav: function() {
			//左侧菜单栏收缩原理、规定一个变量 fig 、fig = true 为展开反之为收缩
			var fig = layui.data('nav')['fig'];
			if(fig) {
				//设置展开
				$("#left-nav").css("width", "150px");
				$("#content").css("width", "calc( 100% - 150px)");
				$(".top-nav-left li:first a i").removeClass("layui-icon layui-icon-spread-left").addClass("layui-icon layui-icon-spread-right");
			} else {
				//设置缩小
				$(".top-nav-left li:first a i").removeClass("layui-icon layui-icon-spread-right").addClass("layui-icon layui-icon-spread-left");
				$("#left-nav").css("width", "0px");
				$("#content").css("width", "100%");
			}
			//主题颜色处理
			var theme = layui.data('skin')['skin-theme'];
			$(parent.document).find("#left-nav").css("background-color",theme);
			$(parent.document).find("#header .logo").css("background-color",theme);
		},
		skin: function(url) {
			$("#theme").click(function(){
				layer.open({
					type: 2,
					title: '主题配色方案',
					area: ['290px', 'calc(100% - 52px)'],
					offset: 'rb',
					shadeClose: true,
					id: 'skin-colors',
					anim: 2,
					shade: 0.2,
					closeBtn: 0,
					isOutAnim: false,
					resize: true,
					move: true,
					content: url
				});
			});
		},
		data_count: function() {
			// 基于准备好的dom，初始化echarts实例
			var myChart = echarts.init(document.getElementById('main'));

			// 指定图表的配置项和数据
			var option = {
				title: {
					text: ''
				},
				tooltip: {
					trigger: 'axis'
				},
				legend: {
					data: ['一周访客统计']
				},
				grid: {
					left: '3%',
					right: '4%',
					bottom: '3%',
					containLabel: true
				},
				toolbox: {
					feature: {
						saveAsImage: {}
					}
				},
				xAxis: {
					type: 'category',
					boundaryGap: true,
					data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
				},
				yAxis: {
					type: 'value'
				},
				series: [{
					name: '一周访客统计',
					type: 'line',
					stack: '总量',
					itemStyle: {
						normal: {
							color:'red',
							lineStyle: {
								color: '#2f4554'
							}
						}
					},
					data: [120, 132, 101, 134, 90, 230, 210]
				}]
			};
			// 使用刚指定的配置项和数据显示图表。
			myChart.setOption(option);
		},
		lock:function($data,$url){
			//获取锁屏状态
			var lock = layui.data('lock')['lock-val'];
			if(lock){
				$(".lock-screen").css("display", "block");
			}else{
				$(".lock-screen").css("display", "none");
			}
			//锁定
			$("#lock").click(function(){
				layui.data('lock', {
					key: 'lock-val',
					value: $data
				});
				$(".lock-screen").css("display", "block");
			});
			//解锁
			$("#get-lock").click(function() {
				if(layui.data("lock")['lock-val']) {
                    $.ajax({
                        type: "post",
                        async: true,
                        url: $url,
                        data: {password:$(".input-div input").val()},
                        dataType: "json",
                        success: function (res) {
                            if(res['status'] == 200) {
                                $(".lock-screen").css("display", "none");
                                layui.data('lock', {
                                    key: 'lock-val',
                                    remove: true
                                });
                            } else {
                                layer.msg(res.msg);
                            }
                        }
                    });
				} else {
					layer.msg("解锁密码错误！");
				}
			});
		}
	}
	exports('home', obj); //注意，这里是模块输出的核心，模块名必须和use时的模块名一致
});